22.5.1 插件打包#
基本打包配置#
// package.json { "name": "my-claude-plugin", "version": "1.0.0", "description": "My Claude Code Plugin", "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ "dist", "plugin.yaml" ], "scripts": { "build": "tsc", "prepack": "npm run build", "pack": "npm pack", "publish": "npm publish" }, "keywords": [ "claude-code", "plugin" ], "author": "Your Name", "license": "MIT", "devDependencies": { "@claude-code/plugin-sdk": "^1.0.0", "typescript": "^4.9.0" } }
TypeScript 配置#
bash
打包脚本#
bash
22.5.2 版本管理#
语义化版本#
bash
22.5.3 文档生成#
API 文档生成#
bash
bash
npm install -g my-claude-plugin
Usage#
Basic Usage#
typescript
}
/**
- 获取指标 */ getMetrics(name: string, limit?: number): Metric[] { const metrics = this.metrics.get(name);
bash
bash
}
/**
- 获取指标统计 */ getStats(name: string): MetricStats | null { const metrics = this.metrics.get(name);
bash
bash
}
/**
- 清除指标 */ clearMetrics(name?: string): void { if (name) { this.metrics.delete(name); } else { this.metrics.clear(); } } }
/**
- 指标 */ interface Metric { name: string; value: number; timestamp: Date; tags: Record<string, string>; }
/**
- 指标统计 */ interface MetricStats { count: number; sum: number; avg: number; min: number; max: number; p50: number; p95: number; p99: number; }
// 使用示例 const monitor = new PerformanceMonitor();
// 记录指标 monitor.record('request.duration', 123, { method: 'GET', endpoint: '/api/users' });
// 测量函数执行时间 const result = await monitor.measure('database.query', async () => { // 数据库查询 return { id: 1, name: 'John' }; });
// 获取指标统计 const stats = monitor.getStats('request.duration'); console.log('Stats:', stats);
22.5.6 用户反馈#
反馈收集#
// src/feedback/feedback-collector.ts /**
- 反馈收集器 / export class FeedbackCollector { private feedbacks: Feedback[] = []; /*
- 收集反馈 / collect(feedback: Omit<Feedback, 'id' | 'timestamp'>): string { const newFeedback: Feedback = { id: this.generateId(), timestamp: new Date(), ...feedback }; this.feedbacks.push(newFeedback); // 发送到反馈服务 this.sendToFeedbackService(newFeedback); return newFeedback.id; } /*
- 生成 ID
/
private generateId(): string {
return
feedback-${Date.now()}-${Math.random().toString(36).substr(2, 9)}; } /* - 发送到反馈服务 / private sendToFeedbackService(feedback: Feedback): void { // 发送到反馈服务 console.log('Sending feedback:', feedback.id); } /*
- 获取反馈 / getFeedbacks(limit?: number): Feedback[] { if (limit) { return this.feedbacks.slice(-limit); } return [...this.feedbacks]; } /*
- 获取反馈统计 / getStats(): FeedbackStats { const stats: FeedbackStats = { total: this.feedbacks.length, byType: {}, byRating: {}, averageRating: 0 }; let totalRating = 0; let ratingCount = 0; for (const feedback of this.feedbacks) { // 按类型统计 stats.byType[feedback.type] = (stats.byType[feedback.type] || 0) + 1; // 按评分统计 if (feedback.rating) { stats.byRating[feedback.rating] = (stats.byRating[feedback.rating] || 0) + 1; totalRating += feedback.rating; ratingCount++; } } // 计算平均评分 if (ratingCount > 0) { stats.averageRating = totalRating / ratingCount; } return stats; } } /*
- 反馈 / interface Feedback { id: string; type: 'bug' | 'feature' | 'improvement' | 'other'; rating?: number; title: string; description: string; user?: string; timestamp: Date; } /*
- 反馈统计 */ interface FeedbackStats { total: number; byType: Record<string, number>; byRating: Record<number, number>; averageRating: number; } // 使用示例 const collector = new FeedbackCollector(); // 收集反馈 const feedbackId = collector.collect({ type: 'feature', rating: 5, title: 'Add new feature', description: 'Please add this feature', user: 'user123' }); console.log('Feedback ID:', feedbackId); // 获取反馈统计 const stats = collector.getStats(); console.log('Feedback stats:', stats);
bash
typescript
// src/feedback/survey.ts
/**
- 用户调查 */ export class UserSurvey { private questions: SurveyQuestion[] = []; private responses: SurveyResponse[] = [];
/**
- 添加问题 */ addQuestion(question: SurveyQuestion): void { this.questions.push(question); }
/**
- 提交响应 */ submitResponse(response: Omit<SurveyResponse, 'id' | 'timestamp'>): string { const newResponse: SurveyResponse = { id: this.generateId(), timestamp: new Date(), ...response };
bash
bash
}
/**
- 生成 ID
*/
private generateId(): string {
return
response-${Date.now()}-${Math.random().toString(36).substr(2, 9)}; }
/**
- 发送到调查服务 */ private sendToSurveyService(response: SurveyResponse): void { // 发送到调查服务 console.log('Sending survey response:', response.id); }
/**
- 获取响应 */ getResponses(limit?: number): SurveyResponse[] { if (limit) { return this.responses.slice(-limit); } return [...this.responses]; }
/**
- 分析响应 */ analyzeResponses(): SurveyAnalysis { const analysis: SurveyAnalysis = { totalResponses: this.responses.length, averageRating: 0, byQuestion: {} };
bash
bash
} }
/**
- 调查问题 */ interface SurveyQuestion { id: string; type: 'text' | 'rating' | 'choice' | 'multiple'; question: string; options?: string[]; required: boolean; }
/**
- 调查响应 */ interface SurveyResponse { id: string; userId?: string; answers: Record<string, any>; timestamp: Date; }
/**
- 调查分析 */ interface SurveyAnalysis { totalResponses: number; averageRating: number; byQuestion: Record<string, any>; }
// 使用示例 const survey = new UserSurvey();
// 添加问题 survey.addQuestion({ id: 'q1', type: 'rating', question: 'How satisfied are you with the plugin?', required: true });
survey.addQuestion({ id: 'q2', type: 'text', question: 'What do you like most about the plugin?', required: false });
survey.addQuestion({ id: 'q3', type: 'choice', question: 'How often do you use the plugin?', options: ['Daily', 'Weekly', 'Monthly', 'Rarely'], required: true });
// 提交响应 const responseId = survey.submitResponse({ userId: 'user123', answers: { q1: 5, q2: 'Easy to use and powerful', q3: 'Daily' } });
console.log('Response ID:', responseId);
// 分析响应 const analysis = survey.analyzeResponses(); console.log('Survey analysis:', analysis);